
local yaoyi = fk.CreateSkill {
  name = "rmt__yaoyiy",
}

Fk:loadTranslationTable{
  ["rmt__yaoyiy"] = "爻疑",
  [":rmt__yaoyiy"] = "锁定技，分发起始手牌时，你的起始手牌背置。你可以将一张背置的牌当一张基本牌或普通锦囊牌使用或打出（每回合每张牌名限一次）。",

  ["#rmt__yaoyiy"] = "爻疑：将一张牌当任意基本牌或普通锦囊牌使用",

  ["$rmt__yaoyiy1"] = "",
  ["$rmt__yaoyiy2"] = "",
}

yaoyi:addEffect("viewas", {
  pattern = ".",
  prompt = "#rmt__yaoyiy",
  interaction = function(self, player)
    local all_names = Fk:getAllCardNames("bt")
    return UI.CardNameBox {
      choices = player:getViewAsCardNames(yaoyi.name, all_names, nil, player:getTableMark("rmt__yaoyiy-turn")),
      all_choices = all_names,
    }
  end,
  handly_pile = true,
  filter_pattern = {
    min_num = 1,
    max_num = 1,
    pattern = ".",
  },
  card_filter = function (self, player, to_select, selected, selected_targets)
    return #selected == 0 and Fk:getCardById(to_select, true):getMark("rmt__yaoyiy-inhand") > 0
  end,
  view_as = function(self, player, cards)
    if #cards ~= 1 or Fk.all_card_types[self.interaction.data] == nil then return end
    local card = Fk:cloneCard(self.interaction.data)
    card:addSubcard(cards[1])
    card.skillName = yaoyi.name
    return card
  end,
  before_use = function(self, player, use)
    player.room:addTableMark(player, "rmt__yaoyiy-turn", use.card.trueName)
  end,
  enabled_at_response = function(self, player, response)
    return #player:getViewAsCardNames(yaoyi.name, Fk:getAllCardNames("bt"), nil, player:getTableMark("rmt__yaoyiy-turn")) > 0
  end,
  enabled_at_nullification = function (self, player, data)
    return table.find(player:getCardIds("h"), function (id)
      return Fk:getCardById(id, true):getMark("rmt__yaoyiy-inhand") > 0
    end) ~= nil and self:enabledAtResponse(player, false)
  end,
})

yaoyi:addEffect("visibility", {
  card_visible = function(self, player, card)
    if Fk:currentRoom():getBanner("RoundCount") == nil then
      --卡个小bug，用来实现刷卡时手牌不可见
      local p = Fk:currentRoom():getCardOwner(card)
      if p and p:hasSkill(yaoyi.name) then
        return player ~= p
      end
    else
      if card:getMark("rmt__yaoyiy-inhand") > 0 then
        return not table.contains(player:getCardIds("h"), card.id)
      end
    end
  end,
  move_visible = function(self, player, info, move)
    if move.to == player and move.skillName == "rmt__chenwei" then
      --FIXME：好像没用？
      return false
    end
  end
})

yaoyi:addEffect("prohibit", {
  prohibit_use = function(self, player, card)
    return card:getMark("rmt__yaoyiy-inhand") > 0
  end,
  prohibit_response = function(self, player, card)
    return card:getMark("rmt__yaoyiy-inhand") > 0
  end,
  prohibit_discard =function(self, player, card)
    --仅处理火攻的情况
    if card:getMark("rmt__yaoyiy-inhand") > 0 then
      if Fk.currentResponseReason == "fire_attack_skill" then
        return true
      elseif RoomInstance then
        local event = RoomInstance.logic:getCurrentEvent()
        if event.event == GameEvent.SkillEffect then
          local data = event.data
          if data.skill.trueName == "fire_attack_skill" then
            return true
          end
        end
      end
    end
  end
})

yaoyi:addEffect("filter", {
  mute = true,
  card_filter = function(self, card, player)
    return table.contains(player:getCardIds("h"), card.id) and card:getMark("rmt__yaoyiy-inhand") > 0
  end,
  view_as = function(self, player, card)
    --通过这种方式禁用special_skills
    local c = Fk:cloneCard(card.name, card.suit, card.number)
    c.special_skills = nil
    return c
  end,
})

yaoyi:addEffect(fk.AfterDrawInitialCards, {
  can_refresh = function (self, event, target, player, data)
    return target == player and player:hasSkill(yaoyi.name, true) and not player:isKongcheng()
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    for _, id in ipairs(player:getCardIds("h")) do
      room:setCardMark(Fk:getCardById(id), "rmt__yaoyiy-inhand", 1)
    end
  end,
})

return yaoyi
